<?php
namespace Home\Model;
//物品基础属性
class Slot extends UserBase{
    protected $redis;
    protected $user;
    protected $type ;
    protected $index ;
    protected $username ;
    
    public function __construct($user){
        parent::__construct($user, 'slot');
        
    }

    public function initData(){
        $this->add('upgrade');
        $this->add('goods');
        $this->add('goods');
        $this->add('worker');
        $this->add('worker');
    }

    public function add($type, $currency = 'gold'){
        $current = $this->hGet($type);
        $current = $current?:0;
        $current ++;
        $info = cfg('slot', $type);
        //升级所需金钱
        $need = $info[$currency][$current-1];
        //升级所需等级
        $needlv = $info['level'][$current-1];
        $money = getClass('money', $this->user);
        if($money->dec($needlv, 'level',  true)===false) return error('等级不足'.$needlv);
        if($money->dec($need, $currency, true)===false) return error('货币不足'.$need);
        //减钱
        $money->dec($need, $currency);
        $pre = getPre($type, $current);
        //加插槽
        $this->hMset(array($pre.'slot_level'=>'1', $type=>$current));
    }

    public function upgrade($type, $index){
        $pre = getPre($type, $index);
        $old = $this->hGet($pre.'slot_level');
        if(!is_numeric($old)) return error('非法插槽');
        if($old > 1) return error('插槽已经升级过了');
        $info = cfg('slot', $type);
        //升级所需钻石
        $need = $info['upgrade'][$index-1];
        $money = getClass('money', $this->user);
        if(!$money->dec($need, 'gem', true)) return error('货币不足'.$need);
        //减钱
        $money->dec($need, 'gem');
        //加插槽
        $this->hMset(array($pre.'slot_level'=>'2'));
    }

    public function getSlot($type, $index){
        $pre = getPre($type, $index);
        $info = $this->hMGet(array($pre.'base_ts', $pre.'ts', $pre.'level', $pre.'name', $pre.'slot_level'));
        if(!is_numeric($info[$pre.'slot_level'])) return error('非法插槽');
        return $info;
    }

    
    public function setSlot($type, $index, $base_ts, $ts, $name, $level){
        $pre = getPre($type, $index);
        return $this->hMset(array($pre.'base_ts'=>$base_ts, $pre.'ts' => $ts
                    , $pre.'level'=>$level, $pre.'name'=>$name));
    }

    public function getFreeSlot($type, $index){
        $slot = $this->getSlot($type, $index);
        $pre = getPre($type, $index);
        if($slot[$pre.'ts']) return error('插槽忙'.$type.$index);
        return $slot;
    }


    //制造完成，结算
    public function finish($type, $index, $ts){
        $pre = getPre($type, $index);
        $slot = $this->getSlot($type, $index);
        if($slot[$pre.'name'] == 'free') return error('空插槽');
        //时间还没到
        if($slot[$pre.'ts'] + $slot[$pre.'base_ts'] > $ts) return error('时间没到');
        if($type == 'goods'){
            return $this->goodsFinish($type, $index, $ts, $slot);
        }elseif($type == 'upgrade'){
            $class = getClass($slot[$pre.'name'], $this->user);
            $class->slotFinish($slot[$pre.'level']);
        }
        
    }

    public function goodsFinish($type, $index, $ts, $slot){
        $pre = getPre($type, $index);
        $name = $slot[$pre.'name'];
        $user = $this->user;        
        //放到储存箱里
        $warehouse = new \Home\Model\Warehouse($this->user);
        if(!$warehouse->add($name, $slot[$pre.'level'], 1, true)) return error('仓库空间不足');
        $warehouse->add($name, $slot[$pre.'level'], 1);
        //加进度
        $goods = getClass('goods', $user, $name);
        $goods->process();
        
        //清理插槽
        $this->setSlot($type, $index, 0, 0, 'free', 0);
        return true;
    }
    
} 